﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Nirvana
{
    [CustomEditor(typeof(MinimapCamera))]
    public class MinimapCameraEditor : Editor
    {
        private sealed class MinimapCameraBind
        {
            public Camera m_camera;
            public MinimapCamera minimapCamera;
            public bool isEnable;
            public bool isActive;

            public void Save()
            {
                Scene activeScene = SceneManager.GetActiveScene();
                string path = activeScene.path;
                if (string.IsNullOrEmpty(path))
                {
                    EditorUtility.DisplayDialog("Notice", "Please save current scene first.","OK");
                    return;
                }

                string filename = path.Substring(0, path.LastIndexOf("."));
                if (!Directory.Exists(filename))
                {
                    Directory.CreateDirectory(filename);
                }

                try
                {
                    string minimapPath = Path.Combine(filename, "minimap.png");
                    if (this.m_camera.SnapShoot(minimapPath))
                    {
                        Debug.Log("Save the minimap to: " + minimapPath);
                        AssetDatabase.Refresh();
                        var mapTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(minimapPath);
                        this.minimapCamera.MapTexture = mapTexture;
                        EditorSceneManager.MarkSceneDirty(activeScene);
                    }
                    else
                    {
                        Debug.LogError("Can not save the minimap to: " + filename);
                    }
                }
                finally
                {
                    this.minimapCamera.SetIgnoreListEnabled(true);
                    this.m_camera.enabled = this.isEnable;
                    this.m_camera.gameObject.SetActive(this.isActive);
                    this.m_camera.targetTexture = null;
                }
            }

        }


        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            MinimapCamera minimap = this.target as MinimapCamera;
            Camera camera = minimap.GetComponent<Camera>();
            if (!camera.orthographic)
            {
                EditorGUILayout.HelpBox("The camer must be orthographic.", MessageType.Error);
            }
            
            if(GUILayout.Button("Take Texture"))
            {
                TakeTexture(minimap, camera);
            }
        }

        private void TakeTexture(MinimapCamera minimap,Camera camera)
        {
            MinimapCameraBind binds = new MinimapCameraBind();
            binds.m_camera = camera;
            binds.minimapCamera = minimap;
            var renderTexture = new RenderTexture(binds.minimapCamera.MapTextureWidth,binds.minimapCamera.MapTextureHeight,32);
            renderTexture.hideFlags = HideFlags.DontSave;
            renderTexture.name = "Mimimap";
            renderTexture.anisoLevel = 9;
            renderTexture.filterMode = FilterMode.Trilinear;
            renderTexture.format = RenderTextureFormat.ARGB32;
            renderTexture.antiAliasing = 8;
            binds.m_camera.targetTexture = renderTexture;
            binds.isEnable = binds.m_camera.enabled;
            binds.isActive = binds.m_camera.gameObject.activeSelf;
            binds.m_camera.enabled = true;
            binds.m_camera.gameObject.SetActive(true);
            binds.minimapCamera.SetIgnoreListEnabled(false);
            EditorApplication.delayCall += new EditorApplication.CallbackFunction(binds.Save);
        }

    }
}

